package com.sloth.controller;

import com.qiniu.util.StringUtils;
import com.sloth.pojo.Customer;
import com.sloth.pojo.RegisterCustomer;
import com.sloth.service.CustomerService;
import com.sloth.util.CookieUtil;
import com.sloth.util.RSAUtils;
import com.sloth.util.ServerResponse;
import com.sloth.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * @author lai
 * @time 2021/10/6 9:05
 */
@Controller
//@RequestMapping("test")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private RedisTemplate redisTemplate;

//
//    @GetMapping("/customer")
//    @ResponseBody
//    public Customer getByCustomerId(Customer customer) {
//        return customerService.getByCustomerId(customer.getCustId());
//    }

    @PostMapping("/cust/login")
    @ResponseBody
    public ServerResponse login(String username, String password, HttpServletRequest request, HttpServletResponse response) {
        return customerService.login(username, password, request, response);
    }

    @GetMapping("/cookie/login")
    @ResponseBody
    public ServerResponse cookielogin(Customer customer) {
            if (customer != null) {
                ServerResponse serverResponse = ServerResponse.getSuccess(customer);
                return serverResponse;
        }
        return ServerResponse.getFail("用户未登录");
    }

    @GetMapping("/cust/logout")
    @ResponseBody
    public ServerResponse logout(@CookieValue("customerTicket") String ticket,HttpServletRequest request ,HttpServletResponse response) {
        System.out.println("logout cookie:"+ticket);
        System.out.println("退出登录");

        redisTemplate.delete("customer:"+ticket);//根据key删除缓存

        Cookie cookie = new Cookie("customerTicket", null);
        cookie.setPath("/");
        cookie.setMaxAge(0); //
        cookie.setHttpOnly(true);
        response.addCookie(cookie);


        return ServerResponse.updateSuccess("退出登录");
    }

    @PostMapping("/register")
    public String register(RegisterCustomer registerCustomer) {
        Customer customer = new Customer();
        customer.setCustPassword(registerCustomer.getCustPassword());
        customer.setCustTelno(registerCustomer.getCustTelno());
        customer.setCustEmail(registerCustomer.getCustEmail());


        System.out.println("密文: " + customer.getCustPassword());
        String encrypted = customer.getCustPassword();

        ServerResponse serverResponse1 = customerService.register(customer);

        if (serverResponse1.getCode() == 200) {
            System.out.println("成功");
            return "redirect:/customer/login.jsp";
        } else {
            System.out.println("失败");
        }
        return null;
    }


    @GetMapping("/publickey")
    @ResponseBody
    public ServerResponse resregister() {

        return customerService.getPublicKey();
    }


    @GetMapping("/encrypted")
    @ResponseBody
    public ServerResponse password(@RequestParam("encrypted") String encrypted) {
        System.out.println("密文: ");
        System.out.println(encrypted);

        String privateKey = customerService.getPrivateKey();

        try {
            System.out.println("解密中.....");
            String decryptedBytes = RSAUtils.privateDecrypt(encrypted, RSAUtils.getPrivateKey(privateKey));
            System.out.println("解密后：" + decryptedBytes);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/create")
    public void createCustomer() {
        customerService.createCustomer(5000);
    }

}